package com.hotacorp.opencloud.authserver.bean;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import com.hotacorp.opencloud.common.authcommon.SysClientBean;

/**
 * OAuth2客户明细接口实现
 * @author lwg
 *
 */
public class SysCientDetail implements ClientDetails {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private SysClientBean sysClientBean;

	public SysCientDetail(SysClientBean sysClientBean) {
		super();
		this.sysClientBean = sysClientBean;
	}

	public void setSysClientBean(SysClientBean sysClientBean) {
		this.sysClientBean = sysClientBean;
	}

	public SysClientBean getSysClientBean() {
		return sysClientBean;
	}

	@Override
	public String getClientId() {
		return sysClientBean.getClientId();
	}

	@Override
	public Set<String> getResourceIds() {
		return sysClientBean.getResourceIds();
	}

	@Override
	public boolean isSecretRequired() {
		return true;
	}

	@Override
	public String getClientSecret() {
		return sysClientBean.getClientSecret();
	}

	@Override
	public boolean isScoped() {
		return true;
	}

	@Override
	public Set<String> getScope() {
		return sysClientBean.getScope();
	}

	@Override
	public Set<String> getAuthorizedGrantTypes() {
		return sysClientBean.getAuthorizedGrantTypes();
	}

	@Override
	public Set<String> getRegisteredRedirectUri() {
		return sysClientBean.getRegisteredRedirectUri();
	}

	@Override
	public Collection<GrantedAuthority> getAuthorities() {
		Collection<GrantedAuthority> dddAuthorities = new ArrayList<>();
		sysClientBean.getAuthorities().forEach(action -> {
			dddAuthorities.add(new GrantedAuthority() {
				private static final long serialVersionUID = 1L;
				public String getAuthority() {
					return action;
				}
			});
		});
		return dddAuthorities;
	}

	@Override
	public Integer getAccessTokenValiditySeconds() {
		return sysClientBean.getAccessTokenValidity();
	}

	@Override
	public Integer getRefreshTokenValiditySeconds() {
		return sysClientBean.getRefreshTokenValidity();
	}

	@Override
	public boolean isAutoApprove(String scope) {
		return sysClientBean.getAutoapprove().contains(scope);
	}

	@Override
	public Map<String, Object> getAdditionalInformation() {
		return null;
	}

	@Override
	public String toString() {
		return "SysCientDetail [sysClientBean=" + sysClientBean + "]";
	}

}
